package com.xh.bussiness.xkw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.model.BookChapter;
import com.xh.bussiness.tiku.model.BookVersion;
import com.xh.bussiness.tiku.model.ChapterKnowledge;
import com.xh.bussiness.tiku.service.IBookChapterService;
import com.xh.bussiness.tiku.service.IBookVersionService;
import com.xh.bussiness.tiku.service.IChapterKnowledgeService;
import com.xh.bussiness.xkw.constant.XkwAPIURL;
import com.xh.bussiness.xkw.dao.XkwChapterCategoryMapper;
import com.xh.bussiness.xkw.model.XkwChapter;
import com.xh.bussiness.xkw.model.XkwChapterCategory;
import com.xh.bussiness.xkw.model.XkwVersionBook;
import com.xh.bussiness.xkw.service.IXkwChapterCategoryService;
import com.xh.bussiness.xkw.service.IXkwChapterService;
import com.xh.bussiness.xkw.service.IXkwVersionBookService;
import com.xh.bussiness.xkw.utils.ConfigUtils;
import com.xh.bussiness.xkw.utils.XkwHttpsUtils;
import com.xh.core.service.impl.BaseServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XkwChapterCategoryServiceImpl extends BaseServiceImpl<XkwChapterCategoryMapper, XkwChapterCategory> implements IXkwChapterCategoryService {

    private final String XkwApiSeriveUrl;

    @Autowired
    public XkwChapterCategoryServiceImpl(ConfigUtils configUtils) {
        XkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
    }

    @Autowired
    private ConfigUtils configUtils;

    @Autowired
    private IXkwVersionBookService xkwVersionBookService;
    @Autowired
    private IXkwChapterService xkwChapterService;
    @Autowired
    private IChapterKnowledgeService chapterKnowledgeService;
    @Autowired
    private IBookChapterService bookChapterService;
    @Autowired
    private IBookVersionService bookVersionService;

    /**
     * 同步学科网章节下的知识点
     *
     * @throws Exception
     */
    public void sysXkwCategoryByChapter() throws Exception {
        List<BookVersion> bookVersions = bookVersionService.queryAll();
        Map<String, Integer> versionIdSubject = new HashMap<>();
        for (BookVersion bookVersion : bookVersions) {
            versionIdSubject.put(bookVersion.getId(), Integer.valueOf(bookVersion.getSubject()));
        }
        List<BookChapter> bookChapters = bookChapterService.queryAll();
        for (BookChapter bookChapter : bookChapters) {
            List<XkwChapterCategory> ccs = queryCategoryByChapterIdAndSubjectId(versionIdSubject.get(bookChapter.getBookVersionId()), Integer.valueOf(bookChapter.getId()));
            if (!ccs.isEmpty()) {
                for (XkwChapterCategory cc : ccs) {
                    ChapterKnowledge chapterKnowledge = new ChapterKnowledge();
                    chapterKnowledge.setChapterId(String.valueOf(cc.getChapterId()));
                    chapterKnowledge.setKnowledgeId(String.valueOf(cc.getXkwId()));
                    chapterKnowledgeService.insert(chapterKnowledge);
                }
            }
        }

    }

    /**
     * 查询学科网章节绑定的知识点
     *
     * @param xkwSubjectId 学科网科目ID
     * @param xkwChapterId 学科网末级章节ID
     */
    private List<XkwChapterCategory> queryCategoryByChapterIdAndSubjectId(Integer xkwSubjectId, Integer xkwChapterId) {
        List<XkwChapterCategory> chapterCategories = new ArrayList<>();
        String queryUrl = String.format(XkwAPIURL.GET_CHAPTER_KNOWLEDGE_BY_SUBJECTID_CHAPTERID, xkwSubjectId, xkwChapterId);
        String url = XkwApiSeriveUrl + queryUrl;
        String categoriesJson = XkwHttpsUtils.doGet(url);
        if (null != categoriesJson) {
            getChapterCategory(xkwChapterId, chapterCategories, categoriesJson);
        }
        return chapterCategories;
    }

    /**
     * 获取章节下的知识点节点
     *
     * @param chapterId         版本ID
     * @param chapterCategories 存储集合
     * @param childJson         子节点Json
     */
    private List<XkwChapterCategory> getChapterCategory(Integer chapterId, List<XkwChapterCategory> chapterCategories, String childJson) {
        List<Map> parse = (List<Map>) JSONArray.parse(childJson);
        if (!parse.isEmpty()) {
            parse.forEach(map -> {
                Integer id = Integer.valueOf(map.get("id").toString());
                String name = map.get("name").toString();
                Integer show = Integer.valueOf(map.get("show").toString());
                XkwChapterCategory chapterCategory = new XkwChapterCategory(id, chapterId, name, show);
                chapterCategories.add(chapterCategory);
            });
        }
        return chapterCategories;
    }

}
